﻿using System;
using InvoiceChecking.Data;
using WebCommon;

public partial class Admin_ReportCashBook : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime fromDate = Convert.ToDateTime(Request.QueryString["FromDate"]);
        DateTime toDate = Convert.ToDateTime(Request.QueryString["ToDate"]);
        string paymentAccount = Request.QueryString["PaymentAccount"];
        string whereClause = string.Format("PaidDate between '{0} 00:00:00' and '{1} 23:59:59' AND IsVoid = 0 AND PaymentTypeID IN (SELECT PaymentTypeID FROM dbo.PaymentType WHERE AccountNumber='{2}')", fromDate.ToString("yyyy-MM-dd"), toDate.ToString("yyyy-MM-dd"), paymentAccount);
        int count;
        var payments = DataRepository.PaymentProvider.GetPaged(whereClause, "PaidDate ASC", 0, Int32.MaxValue, out count);
        var paymentDs = new DSCashBook.PaymentDataTable();
        DataRepository.PaymentProvider.DeepLoad(payments);
        decimal amount = 0;
        foreach (var payment in payments)
        {
            var newPaymentRow = paymentDs.NewPaymentRow();
            newPaymentRow.PaymentID = payment.PaymentId;
            newPaymentRow.PaymentDate = payment.PaymentDate;
            newPaymentRow.ReceivedBy = payment.ReceivedBy;
            newPaymentRow.Description = payment.Description;
            newPaymentRow.LookUpCode = payment.PaymentTypeIdSource.LookUpCode;
            newPaymentRow.PaidDate = payment.PaidDate.Value;
            decimal amountUSD = payment.PaymentTypeIdSource.Currency == "USD" ? payment.Amount.Value : 0;
            decimal amountVND = payment.PaymentTypeIdSource.Currency == "VND" ? payment.Amount.Value : 0;
            newPaymentRow.USDReceive = !payment.IsDebit.Value ? amountUSD : 0;
            newPaymentRow.VNDReceive = !payment.IsDebit.Value ? amountVND : 0;
            newPaymentRow.USDPay = payment.IsDebit.Value ? amountUSD : 0;
            newPaymentRow.VNDPay = payment.IsDebit.Value ? amountVND : 0;
            newPaymentRow.ExchangeRate = payment.ExchangeRate.Value;
            newPaymentRow.USDEqual = amountUSD + amountVND / payment.ExchangeRate.Value;
            newPaymentRow.USDEqual = payment.IsDebit.Value ? newPaymentRow.USDEqual * -1 : newPaymentRow.USDEqual;
            paymentDs.AddPaymentRow(newPaymentRow);
            amount = (decimal) (payment.IsDebit.Value ? amount - payment.Amount : amount + payment.Amount);
        }
        ReportViewer1.LocalReport.DataSources["DSCashBook_Payment"].Value = paymentDs;
        var cashBookDs = new DSCashBook.CashBookDataTable();
        if (count > 0)
        {
            var account = DataRepository.PaymentAccountProvider.GetByPaymentAccount(paymentAccount);
            var newCashBookRow = cashBookDs.NewCashBookRow();
            var firstPayment = payments[0];
            newCashBookRow.FromDate = fromDate;
            newCashBookRow.ToDate = toDate;
            newCashBookRow.OpenBalanceExRate = firstPayment.ExchangeRate.Value;
            newCashBookRow.OpenBalanceUSD = firstPayment.PaymentTypeIdSource.Currency == "USD"
                                                ? firstPayment.OpenBalance.Value
                                                : 0;
            newCashBookRow.OpenBalanceVND = firstPayment.PaymentTypeIdSource.Currency=="VND"? firstPayment.OpenBalance.Value
                                                : 0;
            newCashBookRow.Title = account.Title;
            newCashBookRow.CloseBalanceUSD = firstPayment.PaymentTypeIdSource.Currency == "USD"
                                                ? firstPayment.OpenBalance.Value + amount
                                                : 0;
            newCashBookRow.CloseBalanceVND = firstPayment.PaymentTypeIdSource.Currency == "VND" ? firstPayment.OpenBalance.Value + amount
                                                : 0;
            newCashBookRow.CloseBalanceExRate =  Convert.ToDecimal(SharedFunction.GetVCBHCMExchangeRate());
            cashBookDs.AddCashBookRow(newCashBookRow);

        }
        else
        {
            whereClause = string.Format("PaidDate < '{0} 00:00:00' AND IsVoid = 0 AND PaymentTypeID IN (SELECT PaymentTypeID FROM dbo.PaymentType WHERE AccountNumber='{1}')", fromDate.ToString("yyyy-MM-dd"), paymentAccount);
            payments = DataRepository.PaymentProvider.GetPaged(whereClause, "PaidDate ASC", 0, 1, out count);
            if (count > 0)
            {
                var account = DataRepository.PaymentAccountProvider.GetByPaymentAccount(paymentAccount);
                var newCashBookRow = cashBookDs.NewCashBookRow();
                var firstPayment = payments[0];
                newCashBookRow.FromDate = fromDate;
                newCashBookRow.ToDate = toDate;
                if (firstPayment.IsDebit.Value)
                    firstPayment.OpenBalance += firstPayment.Amount;
                else firstPayment.OpenBalance -= firstPayment.Amount;
                newCashBookRow.OpenBalanceExRate = firstPayment.ExchangeRate.Value;
                newCashBookRow.OpenBalanceUSD = firstPayment.PaymentTypeIdSource.Currency == "USD"
                                                    ? firstPayment.OpenBalance.Value
                                                    : 0;
                newCashBookRow.OpenBalanceVND = firstPayment.PaymentTypeIdSource.Currency == "VND" ? firstPayment.OpenBalance.Value
                                                    : 0;
                newCashBookRow.Title = account.Title;
                newCashBookRow.CloseBalanceUSD = firstPayment.PaymentTypeIdSource.Currency == "USD"
                                                    ? firstPayment.OpenBalance.Value + amount
                                                    : 0;
                newCashBookRow.CloseBalanceVND = firstPayment.PaymentTypeIdSource.Currency == "VND" ? firstPayment.OpenBalance.Value + amount
                                                    : 0;
                newCashBookRow.CloseBalanceExRate = Convert.ToDecimal(SharedFunction.GetVCBHCMExchangeRate());
                cashBookDs.AddCashBookRow(newCashBookRow);
            }
        }
        ReportViewer1.LocalReport.DataSources["DSCashBook_CashBook"].Value = cashBookDs;
        ReportViewer1.DataBind();
    }

}
